package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.InvestOneDetailed;
import com.zzk.lanouhn.myapplication1.modles.InvestOneModle;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lanouhn on 16/8/25.
 */
public class InvestOneAdapter extends RecyclerView.Adapter<InvestOneAdapter.MyViewholder> {
    private Context mContext;
    private List<InvestOneModle.DataBean> list;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public InvestOneAdapter(Context mContext, List<InvestOneModle.DataBean> list) {
        this.mContext = mContext;
        this.list = list;
    }


    @Override
    public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        //找布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_invest_one, null);
        MyViewholder holder = new MyViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewholder holder, final int position) {
        //控件绑定数据
        InvestOneModle.DataBean bean = list.get(position);
        holder.zhuti.setText(bean.getTitle());
        //评论显示那个
        if (bean.getRecommend() == 1) {
            holder.pinglun.setVisibility(View.INVISIBLE);
            holder.iv_hot.setVisibility(View.VISIBLE);
        } else {
            holder.pinglun.setVisibility(View.VISIBLE);
            holder.iv_hot.setVisibility(View.INVISIBLE);
            holder.pinglun.setText("评论 " + bean.getComment());
        }
        //设置时间
        long date = (list.get(position).getPublished()) * 1000L;
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        String dateString = format.format(d);
        holder.shijian.setText(dateString);
        Picasso.with(mContext).load(bean.getPic_url()).into(holder.iv);

        sp = mContext.getSharedPreferences("InvestID", Context.MODE_PRIVATE);
        editor = sp.edit();


        //判断为视频
        final String s = list.get(position).getVideo_url();
        if (!s.equals("")) {
            holder.shipin.setVisibility(View.VISIBLE);
            //点击事件
//            if (investOneOnltemClickLisener != null) {
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //String poss = investOneModle.getData().get(position).getId();
//                        String poss1=investOneModle.getData().get(position).getVideo_url();
//                        //investOneOnltemClickLisener.investOneOntemClick(poss);
//                        investOneOnltemClickLisener.investOneOntemClick(poss1);
//                    }
//                });
//            }
        } else {
            holder.shipin.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pos2 = list.get(position).getId();
                Intent intent = new Intent(mContext, InvestOneDetailed.class);
                intent.putExtra("shipin", s);
                intent.putExtra("shishi", pos2);

                String investId = list.get(position).getId();
                editor.putString("iID", investId);
                editor.commit();

                mContext.startActivity(intent);
            }
        });


        //点击事件
//            if (investOneOnltemClickLisener != null) {
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        String pos = investOneModle.getData().get(position).getId();
//                        investOneOnltemClickLisener.investOneOntemClick(pos);
//                    }
//                });
//            }
//


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewholder extends RecyclerView.ViewHolder {
        private ImageView iv, iv_hot;
        private TextView zhuti, shijian, pinglun, shipin;

        public MyViewholder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.invest_one_iv);
            iv_hot = (ImageView) itemView.findViewById(R.id.invest_one_hot);
            zhuti = (TextView) itemView.findViewById(R.id.invest_one_zhuti);
            shijian = (TextView) itemView.findViewById(R.id.invest_one_shijian);
            pinglun = (TextView) itemView.findViewById(R.id.invest_one_pinglun);
            shipin = (TextView) itemView.findViewById(R.id.invest_one_shiping);

        }
    }
}
